import BTControlNode from "../BTControlNode";
import { FAILURE, RUNNING } from "../BTEnum";
/**
 * 选择节点：
 * 从左到右依次执行其子分支，当第一次遇到子分支返回成功时，
 * 则停止继续执行其他分支，此时Selector节点返回成功，如果所有分支都返回失败时，则Selector节点返回失败
 */
export default class BTSelector extends BTControlNode {
    private _index: number = 0;
    private _firstUpdate: boolean = true;

    start() {
        
    }

    update() {
        let node = this.children[this._index];
        if (this._firstUpdate) {
            node.start();
            this._firstUpdate = false;
        }
        let result = node.update();
        if (result !== RUNNING) {
            node.end();
            if (result === FAILURE && this._index < this.children.length - 1) {
                result = RUNNING;
                this._firstUpdate = true;
                this._index++;
            }
        }
        return result;
    }
}
